﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 地图生成
/// </summary>
public class MapManager : MonoBehaviour {

    private GameObject m_prefab_tile;
    private GameObject m_prefab_wall;
    private GameObject m_prefab_land_spike;
    private GameObject m_prefab_sky_spike;
    private GameObject m_prefab_gem;

    private GameObject m_player;

    private PlayerController m_playercontroller;

    public List<GameObject[]> maplist = new List<GameObject[]>();

    private Transform m_transform;

    private Color m_color_tile;
    private Color m_color_tile2;
    private Color m_color_wall;

    //当前塌陷列数
    private int index=0;

    //生成地物类型
    //0:地砖
    //1:空洞
    //2:地面陷阱
    //3:天空陷阱
    private int item_index;

    private int pr_hole=0 ;
    private int pr_land_spike=0;
    private int pr_sky_spike=0;
    private int pr_gem=2;

    public float bottom_length;

    void Awake() {
        m_prefab_tile = Resources.Load("tile_white") as GameObject;
        m_prefab_wall = Resources.Load("wall2") as GameObject;
        m_prefab_land_spike = Resources.Load("moving_spikes") as GameObject;
        m_prefab_sky_spike = Resources.Load("smashing_spikes") as GameObject;
        m_prefab_gem = Resources.Load("gem 2") as GameObject;

        m_player = GameObject.Find("Player");

        m_playercontroller = GameObject.Find("Player").GetComponent<PlayerController>();

        m_transform = gameObject.GetComponent<Transform>();

        m_color_tile = new Color(124/255f,155/255f,230/255f);
        m_color_tile2 = new Color(125 / 255f, 169 / 255f, 233 / 255f);
        m_color_wall = new Color(105 / 255f, 110 / 255f, 255 / 255f);

        bottom_length = Mathf.Sqrt(2) * 0.254f;

        CreateMapItem(0f);        
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            string str = "";
            for (int i = 0; i < maplist.Count; i++)
            {
                for (int j = 0; j < maplist[i].Length; j++)
                {
                    str += maplist[i][j].name;
                    maplist[i][j].name = i + "---" + j;
                }
                str += "\n";
            }
            Debug.Log(str);
        }
    }

    /// <summary>
    /// 创建地图元素
    /// </summary>
    public void CreateMapItem(float offsetz)
    {
       
        for (int i = 0; i < 15; i++)
        {
            GameObject[] m_tile = new GameObject[6];
            for (int j = 0; j < 6; j++)
            {                                
                Vector3 pos = new Vector3(j * bottom_length , 0, offsetz + i * bottom_length);
                Vector3 rot = new Vector3(-90, 45, 0);
                GameObject tile;
                if (j == 0 || j == 5)
                {
                    tile = GameObject.Instantiate(m_prefab_wall, pos, Quaternion.Euler(rot));
                    tile.GetComponent<MeshRenderer>().material.color = m_color_wall;
                }
                else
                {
                    item_index = CalcPR();
                    if (item_index == 0)
                    {
                        tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot));
                        tile.GetComponent<Transform>().Find("normal_a2").gameObject.GetComponent<MeshRenderer>().material.color = m_color_tile;
                        tile.GetComponent<MeshRenderer>().material.color = m_color_tile;
                        //生成宝石
                        int pr = CalcGemPR();
                        if (pr == 1)
                        {
                            GameObject gogem=GameObject.Instantiate(m_prefab_gem, pos + new Vector3(0, 0.05f, 0), Quaternion.identity);
                            gogem.GetComponent<Transform>().SetParent(tile.GetComponent<Transform>());
                        }
                    }
                    else if(item_index==1)
                    {
                        tile = new GameObject();
                        tile.GetComponent<Transform>().position = pos;
                        tile.GetComponent<Transform>().rotation = Quaternion.Euler(rot);
                    }else if (item_index == 2)
                    {
                        tile = GameObject.Instantiate(m_prefab_land_spike, pos, Quaternion.Euler(rot));
                        //tile.GetComponent<Transform>().Find("moving_spikes_a2").GetComponent<MeshRenderer>().material.color = m_color_tile2;
                    }
                    else if (item_index == 3)
                    {
                        tile = GameObject.Instantiate(m_prefab_sky_spike, pos, Quaternion.Euler(rot));
                        //tile.GetComponent<Transform>().Find("smashing_spikes_a2").GetComponent<MeshRenderer>().material.color = m_color_tile2;
                    }
                    else
                    //这一句 鸡肋 我不知道怎么删除它
                    {
                        tile = new GameObject();
                        tile.GetComponent<Transform>().position = pos;
                        tile.GetComponent<Transform>().rotation = Quaternion.identity;
                    }                    
                }                
                tile.GetComponent<Transform>().SetParent(m_transform);
                m_tile[j] = tile;
            }
            maplist.Add(m_tile);

            GameObject[] m_tile2 = new GameObject[5];
            for (int j = 0; j < 5; j++)
            {
                Vector3 pos = new Vector3(j * bottom_length + bottom_length / 2, 0, offsetz + i * bottom_length + bottom_length / 2);
                Vector3 rot = new Vector3(-90, 45, 0);

                GameObject tile;
                item_index = CalcPR();
                if (item_index == 0)
                {
                    tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot));
                    tile.GetComponent<Transform>().Find("normal_a2").gameObject.GetComponent<MeshRenderer>().material.color = m_color_tile2;
                    tile.GetComponent<MeshRenderer>().material.color = m_color_tile;
                }
                else if (item_index == 1)
                {
                    tile = new GameObject();
                    tile.GetComponent<Transform>().position = pos;
                    tile.GetComponent<Transform>().rotation = Quaternion.Euler(rot);
                }
                else if (item_index == 2)
                {
                    tile = GameObject.Instantiate(m_prefab_land_spike, pos, Quaternion.Euler(rot));
                    //tile.GetComponent<Transform>().Find("moving_spikes_a2").GetComponent<MeshRenderer>().material.color = m_color_tile2;
                }
                else if (item_index == 3)
                {
                    tile = GameObject.Instantiate(m_prefab_sky_spike, pos, Quaternion.Euler(rot));
                    //tile.GetComponent<Transform>().Find("smashing_spikes_a2").GetComponent<MeshRenderer>().material.color = m_color_tile2;
                }
                else
                //这一句 鸡肋 我不知道怎么删除它
                {
                    tile = new GameObject();
                    tile.GetComponent<Transform>().position = pos;
                    tile.GetComponent<Transform>().rotation = Quaternion.identity;
                }                
                tile.GetComponent<Transform>().SetParent(m_transform);
                m_tile2[j] = tile;
            }
            maplist.Add(m_tile2);
        }       
    }

    /// <summary>
    /// 开始下落
    /// </summary>
    public void StartFallDown()
    {
        StartCoroutine("TileFallDown");
    }

    /// <summary>
    /// 停止下落
    /// </summary>
    public void StopFallDown()
    {
        StopCoroutine("TileFallDown");
    }

    /// <summary>
    /// 地板下落
    /// </summary>
    /// <returns></returns>
    private IEnumerator TileFallDown()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.3f);
            for (int i = 0; i < maplist[index].Length; i++)
            {
                Rigidbody rigid=maplist[index][i].AddComponent<Rigidbody>();
                rigid.angularVelocity = new Vector3(Random.Range(0.0f, 1.0f) * 10f, Random.Range(0.0f, 1.0f) * 10f, Random.Range(0.0f, 1.0f) * 10f);
                GameObject.Destroy(maplist[index][i], 1.0f);
            }
            if (m_playercontroller.z == index)
            {
                StopFallDown();
                m_player.AddComponent<Rigidbody>();
                m_playercontroller.StartCoroutine("GameOver", true);
            }
            index++;            
        }
    }

    /// <summary>
    /// 计算概率
    /// 0:地板
    /// 1:空洞
    /// 2:地面陷阱
    /// 3:天空陷阱
    /// </summary>
    /// <returns></returns>
    private int CalcPR()
    {
        int pr = Random.Range(0, 100);
        if (pr < pr_hole)
        {
            return 1;
        }else if (31 < pr && pr < pr_land_spike + 30)
        {
            return 2;
        }else if(61<pr && pr < pr_sky_spike + 60)
        {
            return 3;
        }
        return 0;
    }

    private int CalcGemPR()
    {
        int pr = Random.Range(0, 100);
        if (pr < pr_gem)
        {
            return 1;
        }
        return 0;
    }

    public void AddPR()
    {
        pr_hole += 1;
        pr_land_spike += 1;
        pr_sky_spike += 1;
    }

    public void ResetGameMap()
    {
        Transform[] sonTransform = m_transform.GetComponentsInChildren<Transform>();
        for(int i = 1; i < sonTransform.Length; i++)
        {
            GameObject.Destroy(sonTransform[i].gameObject);
        }

        pr_hole = 0;
        pr_land_spike = 0;
        pr_sky_spike = 0;
        pr_gem = 2;

        index = 0;

        maplist.Clear();

        CreateMapItem(0);
    }
}
